package com.swak.validator.process.impl;

import java.lang.annotation.Annotation;

import com.swak.Constants;
import com.swak.annotation.validate.Idcard;
import com.swak.utils.RegexUtil;
import com.swak.utils.StringUtils;
import com.swak.validator.process.AbstractProcessor;

/**
 * 身份证号验证
 *
 * @author: lifeng
 * @date: 2020/3/29 15:36
 */
public class IdcardProcessor extends AbstractProcessor {

	/**
	 * 处理的类型
	 */
	@Override
	protected Class<?> processBy() {
		return Idcard.class;
	}

	/**
	 * 为 null 判断
	 */
	@Override
	protected boolean doProcess(Annotation check, Object value, String group) {
		Idcard anno = (Idcard) check;
		if (!this.matchGroup(anno.groups(), group)) {
			return true;
		}
		if (value != null && value instanceof String) {
			return RegexUtil.isIdcard((String) value);
		}
		return true;
	}

	/**
	 * 处理为空的异常消息
	 */
	@Override
	protected String processMessage(Annotation ann) {
		Idcard idcard = (Idcard) ann;
		return StringUtils.defaultIfEmpty(idcard.msg(), Constants.IDCARD_ERROR_MSG);
	}
}